package org.example.platform.elasticsearch.service;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.example.platform.elasticsearch.entity.Hotel;
import org.example.platform.elasticsearch.entity.HotelDocRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ESDeleteDocService {
    @Autowired
    private RestHighLevelClient client;

    /**
     * 根据条件删除
     * @param indexName
     * @param city
     * @return
     * @throws Exception
     */
    public String deleteByCity(String indexName, String city) throws Exception {
        DeleteByQueryRequest delete = new DeleteByQueryRequest(indexName);
        delete.setQuery(new TermQueryBuilder("city",city));
        try {
            BulkByScrollResponse response = client.deleteByQuery(delete,RequestOptions.DEFAULT);
            return response.toString();
        } catch (IOException e) {
            log.warn(e.getMessage());
            throw new Exception("按照条件修改服务错误，原因:" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param hotelDocRequest
     * @return
     */
    public String bulkDelete(HotelDocRequest hotelDocRequest) throws Exception {
        String indexName = hotelDocRequest.getIndexName();
        if(CharSequenceUtil.isBlank(indexName)){
            throw new Exception("索引名不能为空");
        }
        List<String> docIds = hotelDocRequest.getHotelList().stream().map(Hotel::getId).collect(Collectors.toList());
        BulkRequest bulkRequest = new BulkRequest();

        for(String docId: docIds){
             DeleteRequest deleteRequest = new DeleteRequest(indexName,docId);
             bulkRequest.add(deleteRequest);
        }
        BulkResponse bulkResponse;
        try{
            bulkResponse = client.bulk(bulkRequest,RequestOptions.DEFAULT);
            if(bulkResponse.hasFailures()){
                return "失败，原因："+bulkResponse.buildFailureMessage();
            }else{
                return "成功";
            }
        }catch (Exception e){
            throw new Exception("搜索错误");
        }
    }

    /**
     * 单个删除
     * @param indexName
     * @param docId
     * @return
     */
    public String singleDelete(String indexName, String docId) {
        if(CharSequenceUtil.isBlank(indexName)){
            throw  new RuntimeException("索引名不能为空");
        }
        DeleteRequest deleteRequest = new DeleteRequest(indexName,docId);

        try {
            DeleteResponse deleteResponse =client.delete(deleteRequest,RequestOptions.DEFAULT);
            return deleteResponse.toString();
        } catch (IOException e) {
            throw new RuntimeException(e+"删除错误");
        }
    }
}
